<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class CservicetCom{
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }
    
    function getAllNumber($customerid, $serviceid){
        try {
            $sql = "select * from c_service where customer_id={$customerid} and service_id={$serviceid}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function changeNumber($customerid, $serviceid, $allnum){
        try {
            $sql = "update c_service set number={$allnum} where customer_id={$customerid} and service_id={$serviceid}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getListByCid($customerid){
        try {
            $sql = "select * from c_service where customer_id={$customerid}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return true;
    }
    
    function getCservice($customerid, $serviceid){
        try {
            $sql = "select * from c_service where customer_id={$customerid} and service_id={$serviceid}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function addCservice($customerid, $serviceid,$number){
        try {
            $sql = "insert into c_service(customer_id,service_id,number) values({$customerid},{$serviceid},{$number})";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllCservice($customerid, $minlimit, $limitnum){
        try{
            $sql = "select * from c_service where customer_id=({$customerid}) order by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumber($customerid){
        try{
            $sql = "select count(1) from c_service where customer_id=({$customerid})";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
}
